
///
/// This class handles the connection between the client and the server stub
/// allocated to the client
///

#ifndef HANDLER_H
#define HANDLER_H

#include "common.h"
#include <cstdio>
#include <string>

#define CLIENT 0
#define PROXY 1

class Handler
{

public:
	
	Handler():mEntityBuf(NULL) {}

	Handler(int fd, unsigned int type) : mSockFD(fd), mEntityBuf(NULL), 
		mEntityBufLength(0), mEntityBufPtr(0), mMsgBufPtr(0), mType(type), 
		mLineHeadersReceived(false), mEntityBytesToEat(0) {}

	Handler(const Handler& rhs);
	Handler& operator=(const Handler& copy);

	~Handler() { DestroyEntityBuffer(); }

	int HandleMessage(const char *packet, const unsigned int packetLength);

	void DestroyEntityBuffer() { if(mEntityBuf != NULL) delete[] mEntityBuf; }

private:
	
	int ProcessHeaderInBuffer();

	int ActOnMessage();

	int mSockFD;

	char mMsgBuf[MSG_BUF_SIZE];

	char* mEntityBuf;
	unsigned int mEntityBufLength;
	unsigned int mEntityBufPtr;

	unsigned int mMsgBufPtr;

	unsigned int mType; // SERVER or CLIENT

	bool mLineHeadersReceived;
	unsigned int mEntityBytesToEat;

	std::string mUri;

	int mOriginFD;
};

#endif
